package com.mdd.car.service.impl;

import com.mdd.car.mapper.CarSeriesMapper;
import com.mdd.car.params.CarSeriesParam;
import com.mdd.car.service.ICarSeriesService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.car.entity.CarSeries;
import com.mdd.car.vo.CarSeriesVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @Description
 * @Author 幻速
 * @Date 2024/3/11 14:22
 **/
@Service
public class CarSeriesServiceImpl extends ServiceImpl<CarSeriesMapper, CarSeries> implements ICarSeriesService {
    @Override
    public List<CarSeriesVo> all() {
        LambdaQueryWrapper<CarSeries> carModelsLQW=new LambdaQueryWrapper<>();
        List<CarSeriesVo> carModelsVos=new ArrayList<>();
        this.baseMapper.selectList(carModelsLQW).forEach(carModels -> {
            CarSeriesVo carSeriesVo=new CarSeriesVo();
            BeanUtil.copyProperties(carModels,carSeriesVo);
            carModelsVos.add(carSeriesVo);
        });
        return carModelsVos;
    }
    @Override
    public PageResult<CarSeriesVo> list(PageValidate pageValidate,String name) {
        int page = null != pageValidate ? pageValidate.getPageNo() : 1;
        int limit = null != pageValidate ? pageValidate.getPageSize() : 10;
        LambdaQueryWrapper<CarSeries> carBrandLQW=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            carBrandLQW.like(CarSeries::getName, name);
        }
        IPage<CarSeries> iPage = this.baseMapper.selectPage(new Page<>(page, limit), carBrandLQW);
        List<CarSeriesVo> list = new LinkedList<>();
        for (CarSeries item : iPage.getRecords()) {
            CarSeriesVo vo = new CarSeriesVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public void add(CarSeriesParam carSeriesParam) {
        CarSeries carSeries=new CarSeries();
        BeanUtils.copyProperties(carSeriesParam,carSeries);
        save(carSeries);
    }

    @Override
    public void edit(CarSeriesParam carSeriesParam) {
        CarSeries carSeries=new CarSeries();
        BeanUtils.copyProperties(carSeriesParam,carSeries);
        updateById(carSeries);
    }

    @Override
    public void del(List<Long> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }
}
